import { createRouter, createWebHashHistory } from "vue-router";
import useUserStore from "@/stores/user";

const router = createRouter({
  //设置为history模式
  history: createWebHashHistory(),
  routes: [
    {
      path: "/home", //配置默认路由
      name: "home", //路由名
      component: () => import("@/pages/home/index.vue"), //引入该路由使用的组件
    },
    {
      path: "/item", //配置默认路由
      name: "item", //路由名
      component: () => import("@/pages/home/item.vue"), //引入该路由使用的组件
    },
    {
      path: "/activity_create", //配置默认路由
      name: "activity_create", //路由名
      component: () => import("@/pages/activity/create.vue"), //引入该路由使用的组件
    },
    {
      path: "/activity_detail", //配置默认路由
      name: "activity_detail", //路由名
      component: () => import("@/pages/activity/detail.vue"), //引入该路由使用的组件
    },
    {
      path: "/activity_list", //配置默认路由
      name: "activity_list", //路由名
      component: () => import("@/pages/activity/list.vue"), //引入该路由使用的组件
    },
    {
      path: "/activity_manage", //配置默认路由
      name: "activity_manage", //路由名
      component: () => import("@/pages/activity/manage.vue"), //引入该路由使用的组件
    },
    {
      path: "/activity_role", //配置默认路由
      name: "activity_role", //路由名
      component: () => import("@/pages/activity/role.vue"), //引入该路由使用的组件
    },
    {
      path: "/score_create", //配置默认路由
      name: "score_create", //路由名
      component: () => import("@/pages/score/create.vue"), //引入该路由使用的组件
    },
    {
      path: "/score_record", //配置默认路由
      name: "score_record", //路由名
      component: () => import("@/pages/score/record.vue"), //引入该路由使用的组件
    },
    {
      path: "/score_record_list", //配置默认路由
      name: "score_record_list", //路由名
      component: () => import("@/pages/score/record_list.vue"), //引入该路由使用的组件
    },
    {
      path: "/score_score_list", //配置默认路由
      name: "score_score_list", //路由名
      component: () => import("@/pages/score/score_list.vue"), //引入该路由使用的组件
    },
    {
      path: "/score", //配置默认路由
      name: "score", //路由名
      component: () => import("@/pages/score/index.vue"), //引入该路由使用的组件
    },
    {
      path: "/", //配置默认路由
      redirect: "/home",
    },
    {
      path: "/search", //配置默认路由
      name: "search", //路由名
      component: () => import("@/pages/home/search.vue"), //引入该路由使用的组件
    },
    {
      path: "/my", //配置默认路由
      name: "my", //路由名
      component: () => import("@/pages/my/index.vue"), //引入该路由使用的组件
    },
    {
      path: "/my_reg", //配置默认路由
      name: "my_reg", //路由名
      component: () => import("@/pages/my/reg.vue"), //引入该路由使用的组件
    },
    {
      path: "/my_perfecting", //配置默认路由
      name: "my_perfecting", //路由名
      component: () => import("@/pages/my/perfecting.vue"), //引入该路由使用的组件
    },
    {
      path: "/school_leave", //配置默认路由
      name: "school_leave", //路由名
      component: () => import("@/pages/school/leave.vue"), //引入该路由使用的组件
    },
    {
      path: "/school_back", //配置默认路由
      name: "school_back", //路由名
      component: () => import("@/pages/school/back.vue"), //引入该路由使用的组件
    },
    {
      path: "/school_list", //配置默认路由
      name: "school_list", //路由名
      component: () => import("@/pages/school/list.vue"), //引入该路由使用的组件
    },
    {
      path: "/school_mylist", //配置默认路由
      name: "school_mylist", //路由名
      component: () => import("@/pages/school/mylist.vue"), //引入该路由使用的组件
    },
    {
      path: "/login", //配置默认路由
      name: "login", //路由名
      component: () => import("@/pages/login/index.vue"), //引入该路由使用的组件
    },
    {
      path: "/error", //配置默认路由
      name: "error", //路由名
      component: () => import("@/pages/error/index.vue"), //引入该路由使用的组件
    },
  ],
  //切换路由后回到顶部
  scrollBehavior() {
    return { left: 0, top: 0 };
  },
});

router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  console.log(to.path);
  console.log(from.path);

  let isLogin = userStore.getLoginStatus;
  if (!isLogin) {
    if (to.path !== "/login" && to.path !== "/my_reg") {
      return next({ path: "/login" });
    } else {
      next();
    }
  } else {
    if (to.path === "/login") {
      return next({ path: "/home" });
    }
    next();
  }
});

export default router;
